package com.ch.engine.design.observe;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体的主题类
 *
 * @author wangchunhui
 * @date 2023/12/12 11:12
 */
public class ConcreteSubject implements Subject {

    private final List<Observer> observers = new ArrayList<>();
    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyObservers();
    }

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void unregisterObserver(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update();
        }
    }
}